<!DOCTYPE html>
<html lang="">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>IP限制</title>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <script src="../common/header.js"></script>
    <!-- 内容主体区域 -->
    <div id="LAY_app_body">
        <div class="layui-fluid">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-header">关开入口</div>
                        <div class="layui-card-body" style="margin: 0 auto;width: 400px">
                            <form class="layui-form layui-form-pane" action="">
                                <div class="layui-form-item">
                                    <label for="entranceSwitch" class="layui-form-label">关开入口</label>
                                    <div class="layui-input-block">
                                        <input type="checkbox" id="entranceSwitch" lay-skin="switch"
                                               name="entranceSwitch" value="0" lay-verify="entranceSwitch"
                                               lay-text="开|关">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <label class="layui-form-label">地址</label>
                                    <div class="layui-input-inline">
                                        <input type="text" lay-verify="entranceContent" id="entranceContent"
                                               name="entranceContent" placeholder="请输入地址" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <a class="layui-btn" lay-submit lay-filter="formSubmit">确认修改</a>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var lastVal = ''
    layui.use(['form', 'layer'], function (form) {
        var $ = layui.$;
        form = layui.form

        form.render()

        form.verify({
            entranceContent: function (value) {
                var entranceSwitchVal = $('#entranceSwitch').prop('checked')
                if (entranceSwitchVal && value.length < 1) {
                    return '请输入地址';
                }
            }
        });

        service.get('mfconfig').then((response) => {
            if (response.data) {
                var data = response.data
                data.forEach((e) => {
                    if (e.id === 4) {
                        if (e.values) {
                            var val = JSON.parse(e.values);
                            $('#entranceContent').val(val.door);
                        } else {
                            $('#entranceContent').val('');
                        }
                        $('#entranceSwitch').attr('checked', val.used === '1');
                        form.render('checkbox')
                        lastVal = e.values
                    }
                })
            } else {
                layer.msg(response.msg, {offset: '45vh', icon: 5, time: 2000});
            }
        }).catch((err) => {
            layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
        })

        //监听提交
        form.on('submit(formSubmit)', function (data) {
            if (lastVal === data.field.entranceContent) {
                layer.msg('请先修改', {offset: '45vh', icon: 5, time: 2000});
            } else {
                var param = {used: 0, door: ''}
                param.used = $('#entranceSwitch').prop('checked') ? 1 : 0
                param.door = data.field.entranceContent
                service.post('api/v1/sys/mfconfig', {
                    id: 4,
                    values: JSON.stringify(param)
                }).then((response) => {
                    if (response.code === 200) {
                        layer.msg('保存成功', {offset: '45vh', icon: 1, time: 2000});
                    } else {
                        layer.msg(response.msg, {offset: '45vh', icon: 5, time: 2000});
                    }
                }).catch((err) => {
                    layer.msg(err.msg, {offset: '45vh', icon: 5, time: 2000});
                })
            }
            return false;
        });
    });
</script>
</body>
</html>
